<template>

	<web-view ref="webViewRef" :src="urlLink" @message="handlePostMessage" allow="*"></web-view>
	<!-- <web-view ref="webViewRef" @message="handlePostMessage" src="http://shop.gluz.com.cn/ts/#/pages/index/index"></web-view> -->
</template>
<script>
	var wv;
	import {
		createSendMessage,
		createC2CConversation
	} from "@/common/public";
	export default {
		data() {
			return {
				statusBarHeight: 0,
				nameType: false,
				urlLink: '',
				paramsData: {}
			}
		},
		mounted() {
			// #ifdef APP-PLUS
				this.$nextTick(() => {
					this.isSetParams(this.paramsData);
				})
			// #endif
			// #ifdef H5
				window.addEventListener('message',(e)=> {
					console.log(e,'參數參數')
					this.handlePostMessage(e.data)
				})
			// #endif
		
		},
		created() {
			let statusBarHeight = JSON.stringify(uni.getSystemInfoSync().statusBarHeight);
			let statusbottom = JSON.stringify(uni.getSystemInfoSync().safeAreaInsets.bottom);
			let lang = uni.getStorageSync('lang');
			let userId = uni.getStorageSync('user_id');
			let xzdToken = uni.getStorageSync('xzdToken');
			let mobile = uni.getStorageSync('mobile');
			this.paramsData = {
				statusBarHeight: statusBarHeight,
				lang: lang,
				userId: userId,
				token: xzdToken,
				mobile: mobile,
				statusbottom: statusbottom
			}
			
			let options = {
				type: '',
				bid: 0,
				offerId: '',
				offer_price: '',
			}

			let {type,bid,offerId,offer_price} = options;
			let url = 'http://192.168.1.30:8081/#/';
			// let url = 'http://shop.gluz.com.cn/ts/#';
			
	
			let urlPin = `?statusBarHeight=${this.paramsData.statusBarHeight}&lang=${this.paramsData.lang}&userId=${this.paramsData.userId}&token=${this.paramsData.token}&mobile=${this.paramsData.mobile}&statusbottom=${this.paramsData.statusbottom}&t=${new Date().getTime()}`;
			if (!type) {
				this.urlLink = url + `/pages/index/index` + urlPin;
				return;
			}
			// type 1 普通详情 2 汽车详情 3 1688 4 店铺详情  app分享功能
			switch(+type){
				case 1:
					url = `${url}/pages/details/shopDetail?id=${bid}`
				break;
				case 2:
					url = `${url}/pages/details/carDetail?id=${bid}`
				break;
				case 3:
					url = `${url}/pages/details/shopDetail1688?id=${bid}&offerId=${offerId}&offer_price=${offer_price}`
				break;
				case 4:
					url = `${url}/pages/details/merchantDetail?bid=${bid}`
				break;
			}
			
			this.urlLink = url + urlPin;


	

		},
		onBackPress(options) {
		  console.log('from:' + options.from)
		},
		methods: {
			isSetParams(data) {
				let currentWebview = this.$scope.$getAppWebview();
				
				try {
					wv = currentWebview.children()[0];
					currentWebview.setStyle({
						'popGesture':  'none',
					})
					wv.addEventListener('loaded', function() {
						wv.evalJS(`setParams(${JSON.stringify(data)})`);
					})

				} catch (e) {
					//TODO handle the exception
					console.log("e=============", e)
				}

			},
			goback() {
				uni.switchTab({
					url: '/pages/tabbar/index'
				});
			},
			async handlePostMessage(data) {
				let recieveObj = {};
				// #ifdef APP-PLUS
				console.log("接收到消息：" + JSON.stringify(data.detail.data));
					recieveObj = data.detail.data[0]
				// #endif

				// #ifdef H5
					recieveObj = data
				// #endif

				// 切换竖屏
        // #ifdef APP-PLUS
          if (recieveObj.name === "setLockOrientation") {
            plus.screen.lockOrientation(recieveObj.params);
          }
        // #endif
				if (recieveObj.name === "goback") {
					this.goback();
				}
				if (recieveObj.name === "login") {
					if (!!this.nameType) return;
					this.nameType = true;
					uni.redirectTo({
						url: "/pages/user/login/login"
					})
					this.nameType = false;
				}
				if (recieveObj.name === "call") {
					let tel = recieveObj.tel;
					// createC2CConversation("15607593523", this)
					createC2CConversation(tel, this)
				}
				if (recieveObj.name === "meeting") {
					let tel = recieveObj.tel;
					let username =  recieveObj.username;
					// 自定义卡片内容
					let text = `${username}向你申请进行视频会议请前往多语管家创建会议并邀请“${username}请及时与“${username}”进行沟通视频会议时 间等相关事项，无限商机，尽在GULZ`
					createSendMessage(tel, text, this)
				}
			},
		}
	}
</script>

<style scoped lang="less">

</style>